package org.jivesoftware.smackx.provider;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Collections;
import java.util.Comparator;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.TimeZone;
import org.jivesoftware.smack.packet.Packet;
import org.jivesoftware.smack.provider.PacketExtensionProvider;
import org.jivesoftware.smackx.packet.DelayInformation;

public class DelayInformationProvider
  implements PacketExtensionProvider
{
  private static final SimpleDateFormat XEP_0082_UTC_FORMAT_WITHOUT_MILLIS;
  private static final SimpleDateFormat XEP_0091_UTC_FALLBACK_FORMAT = new SimpleDateFormat("yyyyMd'T'HH:mm:ss");
  private static Map<String, DateFormat> formats;

  static
  {
    XEP_0091_UTC_FALLBACK_FORMAT.setTimeZone(TimeZone.getTimeZone("UTC"));
    XEP_0082_UTC_FORMAT_WITHOUT_MILLIS = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'");
    XEP_0082_UTC_FORMAT_WITHOUT_MILLIS.setTimeZone(TimeZone.getTimeZone("UTC"));
    formats = new HashMap();
    formats.put("^\\d+T\\d+:\\d+:\\d+$", DelayInformation.XEP_0091_UTC_FORMAT);
    formats.put("^\\d+-\\d+-\\d+T\\d+:\\d+:\\d+\\.\\d+Z$", Packet.XEP_0082_UTC_FORMAT);
    formats.put("^\\d+-\\d+-\\d+T\\d+:\\d+:\\d+Z$", XEP_0082_UTC_FORMAT_WITHOUT_MILLIS);
  }

  private Calendar determineNearestDate(Calendar paramCalendar, List<Calendar> paramList)
  {
    Collections.sort(paramList, new Comparator(this, paramCalendar)
    {
      public int compare(, Calendar paramCalendar2)
      {
        return new Long(this.val$now.getTimeInMillis() - paramCalendar1.getTimeInMillis()).compareTo(new Long(this.val$now.getTimeInMillis() - paramCalendar2.getTimeInMillis()));
      }
    });
    return ((Calendar)paramList.get(0));
  }

  private List<Calendar> filterDatesBefore(Calendar paramCalendar, Calendar[] paramArrayOfCalendar)
  {
    ArrayList localArrayList = new ArrayList();
    int i = paramArrayOfCalendar.length;
    int j = 0;
    while (true)
    {
      if (j >= i)
        return localArrayList;
      Calendar localCalendar = paramArrayOfCalendar[j];
      if ((localCalendar != null) && (localCalendar.before(paramCalendar)))
        localArrayList.add(localCalendar);
      ++j;
    }
  }

  private Date handleDateWithMissingLeadingZeros(String paramString)
  {
    Date localDate;
    GregorianCalendar localGregorianCalendar = new GregorianCalendar();
    Calendar localCalendar1 = parseXEP91Date(paramString, DelayInformation.XEP_0091_UTC_FORMAT);
    Calendar localCalendar2 = parseXEP91Date(paramString, XEP_0091_UTC_FALLBACK_FORMAT);
    Calendar[] arrayOfCalendar = new Calendar[2];
    arrayOfCalendar[0] = localCalendar1;
    arrayOfCalendar[1] = localCalendar2;
    List localList = filterDatesBefore(localGregorianCalendar, arrayOfCalendar);
    if (!(localList.isEmpty()))
      localDate = determineNearestDate(localGregorianCalendar, localList).getTime();
    while (true)
    {
      return localDate;
      localDate = null;
    }
  }

  private Calendar parseXEP91Date(String paramString, DateFormat paramDateFormat)
  {
    try
    {
      monitorenter;
    }
    catch (ParseException localParseException)
    {
      Calendar localCalendar;
      try
      {
        paramDateFormat.parse(paramString);
        localCalendar = paramDateFormat.getCalendar();
        monitorexit;
        break label30:
      }
      finally
      {
        localObject = finally;
        monitorexit;
        throw localObject;
        localParseException = localParseException;
        localCalendar = null;
      }
      label30: return localCalendar;
    }
  }

  // ERROR //
  public org.jivesoftware.smack.packet.PacketExtension parseExtension(org.xmlpull.v1.XmlPullParser paramXmlPullParser)
    throws java.lang.Exception
  {
    // Byte code:
    //   0: aload_1
    //   1: ldc 141
    //   3: ldc 143
    //   5: invokeinterface 149 3 0
    //   10: astore_2
    //   11: aconst_null
    //   12: astore_3
    //   13: aconst_null
    //   14: astore 4
    //   16: getstatic 48	org/jivesoftware/smackx/provider/DelayInformationProvider:formats	Ljava/util/Map;
    //   19: invokeinterface 153 1 0
    //   24: invokeinterface 159 1 0
    //   29: astore 5
    //   31: aload 5
    //   33: invokeinterface 164 1 0
    //   38: ifne +101 -> 139
    //   41: aload 4
    //   43: getstatic 56	org/jivesoftware/smackx/packet/DelayInformation:XEP_0091_UTC_FORMAT	Ljava/text/DateFormat;
    //   46: if_acmpne +25 -> 71
    //   49: aload_2
    //   50: ldc 166
    //   52: invokevirtual 172	java/lang/String:split	(Ljava/lang/String;)[Ljava/lang/String;
    //   55: iconst_0
    //   56: aaload
    //   57: invokevirtual 176	java/lang/String:length	()I
    //   60: bipush 8
    //   62: if_icmpge +9 -> 71
    //   65: aload_0
    //   66: aload_2
    //   67: invokespecial 178	org/jivesoftware/smackx/provider/DelayInformationProvider:handleDateWithMissingLeadingZeros	(Ljava/lang/String;)Ljava/util/Date;
    //   70: astore_3
    //   71: aload_3
    //   72: ifnonnull +11 -> 83
    //   75: new 180	java/util/Date
    //   78: dup
    //   79: invokespecial 181	java/util/Date:<init>	()V
    //   82: astore_3
    //   83: new 52	org/jivesoftware/smackx/packet/DelayInformation
    //   86: dup
    //   87: aload_3
    //   88: invokespecial 184	org/jivesoftware/smackx/packet/DelayInformation:<init>	(Ljava/util/Date;)V
    //   91: astore 8
    //   93: aload 8
    //   95: aload_1
    //   96: ldc 141
    //   98: ldc 186
    //   100: invokeinterface 149 3 0
    //   105: invokevirtual 189	org/jivesoftware/smackx/packet/DelayInformation:setFrom	(Ljava/lang/String;)V
    //   108: aload_1
    //   109: invokeinterface 193 1 0
    //   114: astore 9
    //   116: ldc 141
    //   118: aload 9
    //   120: invokevirtual 196	java/lang/String:equals	(Ljava/lang/Object;)Z
    //   123: ifeq +6 -> 129
    //   126: aconst_null
    //   127: astore 9
    //   129: aload 8
    //   131: aload 9
    //   133: invokevirtual 199	org/jivesoftware/smackx/packet/DelayInformation:setReason	(Ljava/lang/String;)V
    //   136: aload 8
    //   138: areturn
    //   139: aload 5
    //   141: invokeinterface 203 1 0
    //   146: checkcast 168	java/lang/String
    //   149: astore 6
    //   151: aload_2
    //   152: aload 6
    //   154: invokevirtual 207	java/lang/String:matches	(Ljava/lang/String;)Z
    //   157: ifeq -126 -> 31
    //   160: getstatic 48	org/jivesoftware/smackx/provider/DelayInformationProvider:formats	Ljava/util/Map;
    //   163: aload 6
    //   165: invokeinterface 210 2 0
    //   170: checkcast 128	java/text/DateFormat
    //   173: astore 4
    //   175: aload 4
    //   177: monitorenter
    //   178: aload 4
    //   180: aload_2
    //   181: invokevirtual 131	java/text/DateFormat:parse	(Ljava/lang/String;)Ljava/util/Date;
    //   184: astore_3
    //   185: aload 4
    //   187: monitorexit
    //   188: goto -147 -> 41
    //   191: astore 10
    //   193: aload 4
    //   195: monitorexit
    //   196: aload 10
    //   198: athrow
    //   199: astore 7
    //   201: goto -160 -> 41
    //
    // Exception table:
    //   from	to	target	type
    //   178	196	191	finally
    //   160	178	199	java/text/ParseException
    //   196	199	199	java/text/ParseException
  }
}